package com.be.beadmin.generator.controller;

import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.PageUtil;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.generator.dto.input.GenDownDto;
import com.be.beadmin.generator.dto.input.GenDtoPage;
import com.be.beadmin.generator.entity.ColumnInfo;
import com.be.beadmin.generator.service.GenConfigService;
import com.be.beadmin.generator.service.GeneratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统代码生成管理
 */
@RestController
public class GeneratorController {

    @Autowired
    GeneratorService generatorService;

    @Autowired
    GenConfigService genConfigService;

    /**
     * 查询数据库
     * @return
     */
    @GetMapping(value = "/generator/tables/all")
    public ResultModel queryAllTables(){
        return  ResultModel.success(generatorService.getTables());
    }

    /**
     * 查询数据库字段
     * @param genDtoPage
     * @return
     */
    @PostMapping("/generator/tables")
    public ResultModel getMenuByPage(@RequestBody GenDtoPage genDtoPage){
        int[] startEnd = PageUtil.transToStartEnd(genDtoPage.getCurrent() -1, genDtoPage.getPageSize());
        return ResultModel.success(generatorService.getTables(genDtoPage.getName(), startEnd));
    }


    /**
     * 查询数据库字段
     * @param tableName
     * @return
     */
    @PostMapping(value = "/generator/columns")
    public ResultModel queryColumns(@RequestBody String tableName){
        List<ColumnInfo> columnInfos = generatorService.getColumns(tableName);
        return ResultModel.success(PageUtil.toPage(columnInfos,columnInfos.size()));
    }


    /**
     * 保存数据库字段
     * @param columnInfos
     * @return
     */
    @PostMapping(value = "/generator/saveColumns")
    public ResultModel saveColumn(@RequestBody List<ColumnInfo> columnInfos){
        generatorService.save(columnInfos);
        return ResultModel.success("保存成功");
    }


    /**
     * 同步数据库字段
     * @param tables
     * @return
     */
    @PostMapping(value = "/generator/sync")
    public ResultModel syncColumn(@RequestBody List<String> tables){
        for (String table : tables) {
            generatorService.sync(generatorService.getColumns(table), generatorService.query(table));
        }
        return ResultModel.success(ResultModel.UPDATE);
    }

    /**
     * 代码生成
     * @param genDownDto
     * @param request
     * @param response
     * @return
     */
    @PostMapping(value = "/generator/genOrDownload")
    public ResultModel generatorCode(@RequestBody GenDownDto genDownDto, HttpServletRequest request, HttpServletResponse response){
//        if(!generatorEnabled && type == 0){
//            throw new BadRequestException("此环境不允许生成代码，请选择预览或者下载查看！");
//        }
        switch (genDownDto.getType()){
            // 生成代码
            case 0: generatorService.generator(genConfigService.find(genDownDto.getTableName()), generatorService.getColumns(genDownDto.getTableName()));
                return ResultModel.success("生成成功");

            // 预览
            case 1: return ResultModel.success(generatorService.preview(genConfigService.find(genDownDto.getTableName()), generatorService.getColumns(genDownDto.getTableName())));
            // 打包
            case 2: generatorService.download(genConfigService.find(genDownDto.getTableName()), generatorService.getColumns(genDownDto.getTableName()), request, response);
                break;
            default: throw new BeadminException("没有这个选项");
        }
        return  null;
    }


}
